package org.zjvis.datascience.common.widget.vo;

import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.zjvis.datascience.common.util.DataUtil;
import org.zjvis.datascience.common.widget.WidgetDTOUtil;
import org.zjvis.datascience.common.widget.WordGeneratorUtil;
import org.zjvis.datascience.common.widget.constant.WordTemplateConstant;
import org.zjvis.datascience.common.widget.dto.WidgetDTO;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static org.zjvis.datascience.common.widget.constant.WordTemplateConstant.*;


/**
 * @description TODO
 * @date 2022/1/26
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class WordReportVO {

    private String fileName; //待生成的word文件的名字

    private Map<String, String> dataMap;

    private String templateName; //根据模板生成word文件

    private String LATEST_DATA_DATE = "LATEST_DATA_DATE";

    private String GENERATED_DATE = "GENERATED_DATE";

    private String CATALOG_NAME_PREFIX = "CATALOG_";

    private String CATALOG_INDEX = "CATALOG_INDEX";

    private String CATALOG_NAME = "CATALOG_NAME";

    private String CATALOG_PAGE_NUM = "CATALOG_PAGE_NUM";

    private StringBuffer CONTENT = new StringBuffer();

    private StringBuffer IMAGE_HEADER = new StringBuffer();

    private StringBuffer IMAGE_TAGS = new StringBuffer();

    public WordReportVO(String templateName) {
        this.dataMap = Maps.newHashMap();
        this.templateName = templateName;
        Date currentDate = new Date();
        this.fileName = templateName + "[" + new SimpleDateFormat("yyyy-M-d").format(currentDate) + "]";
        this.dataMap.put(WordTemplateConstant.LATEST_DATA_DATE, new SimpleDateFormat("yyyy年M月").format(currentDate));
        this.dataMap.put(WordTemplateConstant.GENERATED_DATE, DataUtil.formatChineseDateStr(currentDate));
    }

    public void fileContent(List<WidgetDTO> widgets, Map<String, String> imgUrlMap) {
        int imgIndex = 1;
        for (WidgetDTO widget: widgets){
            if (widget.isTextType()){
                String content = WidgetDTOUtil.getValueByKey("chartOptions.value", widget, String.class);
                this.getCONTENT().append(String.format(TEXT_SNIPPET, WordGeneratorUtil.wrapText(content)));
            }else if(widget.isChartType()) {
                String imgName = WidgetDTOUtil.getValueByKey("i", widget, String.class);
                WordImageVO wordImageVO = new WordImageVO(imgIndex, imgName, imgUrlMap.getOrDefault(widget.getId().toString(),"http://localhost:8080/icon/components/barChart.png"));
                this.getIMAGE_HEADER().append(String.format(IMAGE_REF_ID, wordImageVO.getRefId(), wordImageVO.getFullName()));
                this.getIMAGE_TAGS().append(String.format(IMAGE_TAG, wordImageVO.getFullName(), wordImageVO.getImageType(), wordImageVO.getBase64Code()));
                this.getCONTENT().append(wordImageVO.toXML(imgIndex++, widget.getName()));
            }
            this.getCONTENT().append(WordTemplateConstant.LINE_BREAK_SNIPPET);
        }
        this.dataMap.put(WordTemplateConstant.CATALOG_INDEX, this.getCATALOG_INDEX());
        this.dataMap.put(WordTemplateConstant.CATALOG_NAME, this.getCATALOG_NAME());
        this.dataMap.put(WordTemplateConstant.CATALOG_PAGE_NUM, this.getCATALOG_PAGE_NUM());
        this.dataMap.put(WordTemplateConstant.IMAGE_HEADER, this.getIMAGE_HEADER().toString());
        this.dataMap.put(WordTemplateConstant.CONTENT, this.getCONTENT().toString());
        this.dataMap.put(WordTemplateConstant.IMAGE_TAGS, this.getIMAGE_TAGS().toString());
    }

    public File generateFile(){
        return WordGeneratorUtil.createDoc(this.getTemplateName(), this.getFileName(), this.getDataMap());
    }


}
